package com.yoda.mall.admin.controller;

import com.yoda.mall.admin.service.CouponService;
import com.yoda.mall.model.dto.CouponDto;
import com.yoda.mall.model.entity.sms.Coupon;
import com.yoda.mall.model.vo.CouponVo;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    //添加优惠券
    @PostMapping("create")
    public R createCoupon(@RequestBody CouponDto couponDto) {
        couponService.createCoupon(couponDto);
        return R.success();
    }

    //删除优惠券
    @PostMapping("delete/{id}")
    public R deleteCoupon(@PathVariable Long id) {
        couponService.deleteCoupon(id);
        return R.success();
    }

    //根据优惠券名称和类型分页获取优惠券列表
    @GetMapping("list")
    public R couponList(String name, Integer pageNum, Integer pageSize, Integer type) {
        PageResult<Coupon> pageResult = couponService.couponListPage(name, pageNum, pageSize, type);
        return R.success(pageResult);
    }

    //修改优惠券
    @PostMapping("update/{id}")
    public R updateCoupon(@RequestBody CouponDto couponDto, @PathVariable Long id) {
        couponDto.setId(id);
        couponService.updateCoupon(couponDto);
        return R.success();
    }

    //获取单个优惠券的详细信息
    @GetMapping("{id}")
    public R getCouponInfo(@PathVariable Long id) {
        CouponVo couponVo = couponService.getCouponInfo(id);
        return R.success(couponVo);
    }
}
